﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EntityObjects;
using DAL_V1.App_Code.DBBroker;

namespace BAL.Controller
{
    public class ConsolidateRetrivalControl
    {
        CatalogueDBBroker catalogueRef;
        RequisitionDBBroker requisitionRef;
        DisbursementDBBroker disbursementRef;
        DepartmentDBBroker departmentRef;
        Boolean submitFlag=true;
        public ConsolidateRetrivalControl()
        {
            catalogueRef = CatalogueDBBroker.getCatalogueDBBroker();
            requisitionRef = RequisitionDBBroker.getRequisitionDBBroker();
            disbursementRef = DisbursementDBBroker.getDisbursementDBBroker();
            departmentRef = DepartmentDBBroker.getDepartmentDBBroker();
        }
        public List<Category> getAllCategory()
        {
            List<Category> list = catalogueRef.selectAllCategory();
            return (list == null) ? null : list;
        }

        public List<Retrival> getRetrievalList(int catId)
        {
            List<Retrival> retrivalList = new List<Retrival>();
            retrivalList = requisitionRef.getItemsForRetrival(catId);

            return retrivalList;
        }
        public Catalogue getCatalogueByItem(string itemid)
        {
            Catalogue list = catalogueRef.selectCatalogue(itemid);
            return list;
        }
        public Category getCategoryById(int id)
        {
            return catalogueRef.selectCategory(id);
        }

        //assign retrieval items
        public List<ConsolidateRetrivalItem> getDataForAssignment(string itemid)
        {
            List<ConsolidateRetrivalItem> totQtyList = requisitionRef.getRequisitionDatabyItemId(itemid);
            List<UnCollectedTime>  UnCollTimeList= disbursementRef.getUnCollectedTime();
            List<Retrival> UnfulfiledList = requisitionRef.getRequisitionByItemId(itemid);
            List<DisbursementCatalogue> assignedList = disbursementRef.getDisbursedItemByItemIdAndAssignedStatus(itemid);

            //calculate outstanding qty
            if(UnfulfiledList!=null)
            foreach (Retrival r in UnfulfiledList)
            {
                if (r.ReceivedQty != 0 && r.ReceivedQty!=r.RequestedQty)
                {
                    r.OutstandingQty = r.RequestedQty - r.ReceivedQty;
                }
            }
            //calculate total outstanding qty
            if(totQtyList!=null)
            foreach (ConsolidateRetrivalItem c in totQtyList)
            {
                c.TotalOutStandingQty = c.TotalReqQty - c.TotalRecQty;
            }


            List<ConsolidateRetrivalItem> gridDataList = new List<ConsolidateRetrivalItem>();
            gridDataList = totQtyList;
            //concat uncollected time qty according to dept
            if (UnCollTimeList != null)
            {

                foreach (UnCollectedTime c in UnCollTimeList)
                {
                    foreach (ConsolidateRetrivalItem rt in gridDataList)
                    {
                        if (c.DeptId == rt.DeptId)
                            rt.UnCollectedTime = c.UnCollectedNo;
                    }
                }
            }

            //concat unfulfiled qty according to dept
            if (UnfulfiledList != null)
            {
                foreach (Retrival r in UnfulfiledList)
                {
                    foreach (ConsolidateRetrivalItem rt in gridDataList)
                    {
                        if (rt.DeptId == r.DeptId)
                        {
                            rt.UnFullFilledQty = r.OutstandingQty;
                        }
                    }
                }
            }
            if (assignedList != null)
            {
                foreach (DisbursementCatalogue dc in assignedList)
                {
                    foreach (ConsolidateRetrivalItem rt in gridDataList)
                    {
                        if (rt.DeptId == dc.DeptId)
                        {
                            rt.AssignedQty = dc.ReceivedQty;
                            rt.Status = dc.Status;
                        }
                    }
                }
            }
            //to get this week qty
            List<ConsolidateRetrivalItem> resultList = new List<ConsolidateRetrivalItem>();
            foreach (ConsolidateRetrivalItem c in gridDataList)
            {
                c.ThisWeekQty = c.TotalOutStandingQty - c.UnFullFilledQty;
                if (c.AssignedQty == 0)
                {
                    
                    resultList.Add(c);
                    c.Flag = true;
                }
                else
                {
                    c.Flag = false;
                }

            }

           
           return resultList;
           // return gridDataList;

        }

        //update qty in catalogue
        public Boolean updateRetrievedQtyinCatalogue(string itemid,int qty)
        {

            Catalogue c = catalogueRef.selectCatalogue(itemid);
            c.TotalQty = c.TotalQty - qty;
            int row = catalogueRef.updateCatalogueTotalQty(c,itemid);
            if (row == 1)
                return true;
            else
                return false;
        }

        
        public void SaveDisbursement(List<Disbursement> list)
        {
            foreach (Disbursement d in list)
            {
                Disbursement rst = disbursementRef.getDisbursementByDeptIdAndAssignedStatus(d.DeptId);
                if (rst == null)
                     disbursementRef.addDisbursement(d);
            }
        }

       
        public void SaveDisbursementDetail(List<DisbursementDetails> list)
        {
            foreach (DisbursementDetails d in list)
            {
                Disbursement rst = disbursementRef.getDisbursementByDeptIdAndAssignedStatus(d.DeptId);
                d.DisbursementId = rst.DisbursementId;
                disbursementRef.addDisbursementDetails(d);
            }
        }

        public Department getDepartment(string deptId)
        {
            return departmentRef.selectDepartment(deptId);
        }
    }
}
